package com.zwj.learning.timetable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.common.entity.Student;
import com.zwj.common.service.StudentService;
import com.zwj.common.service.UserService;
import com.zwj.learning.timetable.common.TokenThreadLocal;
import com.zwj.learning.timetable.constant.RedisKey;
import com.zwj.learning.timetable.entity.Grade;
import com.zwj.learning.timetable.entity.SchoolOpen;
import com.zwj.learning.timetable.service.GradeService;
import com.zwj.learning.timetable.service.SchoolOpenService;
import com.zwj.learning.timetable.mapper.SchoolOpenMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
* @author 23088
* @description 针对表【school_open(各学期开学时间)】的数据库操作Service实现
* @createDate 2022-11-20 22:30:52
*/
@Service
public class SchoolOpenServiceImpl extends ServiceImpl<SchoolOpenMapper, SchoolOpen>
    implements SchoolOpenService{

    @DubboReference
    private UserService userService;

    @DubboReference
    private StudentService studentService;

    @Resource
    private GradeService gradeService;

    @Override
    public SchoolOpen getSchoolOpenTime(String semester) {

        String currentToken = TokenThreadLocal.getCurrentToken();
        Long loginUserId = userService.getLoginUserId(currentToken);
        Student student = studentService.getStudentById(loginUserId);

        LambdaQueryWrapper<Grade> gradeWrapper = new LambdaQueryWrapper<>();
        gradeWrapper.eq(Grade::getGrade, student.getGrade());
        Grade grade = gradeService.getOne(gradeWrapper);

        LambdaQueryWrapper<SchoolOpen> schoolOpenWrapper = new LambdaQueryWrapper<>();
        schoolOpenWrapper.eq(SchoolOpen::getSemesterId, Integer.valueOf(semester))
                .eq(SchoolOpen::getGradeId, grade.getId());

        return this.getOne(schoolOpenWrapper);
    }
}




